
$(function() {
    validateRule();


});

// $.validator.setDefaults({
//     submitHandler: function(form) {
//         console.log(form);
//         debugger;
//         register();
//     }
// });
function register() {
    $.modal.loading($("#btnSubmit").data("loading"));
    $.ajax({
        type: "post",
        url: ctx + "partner/dept/register",
        data: $('#form-dept-add').serialize(),
        success: function(r) {
            if (r.code == web_status.SUCCESS) {
            	layer.alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", {
            	    icon: 1,
            	    title: "系统提示"
            	},
            	function(index) {
            	    //关闭弹窗
            	    layer.close(index);
            	    location.href = ctx + 'login';
            	});
            } else {
            	$.modal.closeLoading();
            	$.modal.msg(r.msg);
            }
        }
    });
}

function validateRule() {
    $("#registerForm").validate({
        onkeyup: false,
        rules:{
            deptName:{
                remote: {
                    url: ctx + "partner/dept/checkDeptUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "deptName" : function() {
                            return $.common.trim($("#deptName").val());
                        }
                    }
                }
            },
            code:{
                remote: {
                    url: ctx + "partner/dept/checkDeptUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "code" : function() {
                            return $.common.trim($("#code").val());
                        }
                    }
                }
            },
            loginName:{
                remote: {
                    url: ctx + "system/user/checkLoginNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "loginName" : function() {
                            return $.common.trim($("#loginName").val());
                        }
                    }
                }
            },
            phone:{
                remote: {
                    minlength: 11,
                    maxlength: 11,
                    url: ctx + "system/user/checkPhoneUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "phone" : function() {
                            return $.common.trim($("#phone").val());
                        }
                    }
                }
            },
            password:{
                minlength: 6,
                maxlength: 20
            },
            focusCleanup: true
        },
        messages: {
            "deptName": {
                remote: "企业名称已经存在"
            },
            "code": {
                remote: "企业税号已经存在"
            },
            "loginName": {
                remote: "用户已经存在"
            },
            "phone":{
                remote: "手机号码已经存在"
            }

        },
    })
}
